<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
require_once('Model/Pager.php');
require_once('Plugin/Page.php');

$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$page   = new Model_page();
$smarty->assign('page', $page);

if (isset($params['do']) && function_exists('exec_' . $params['do'])){
   $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$page);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$page);
}
else{
    call_user_func('op_search', $params, &$smarty, &$page);
}


function op_search($params, &$smarty, &$page)
{
    if (!isset($params['search'])){
        $params['search']   = array();
    }else{
        $page_id = isset($params['page_id']) && $params['page_id'] > 1 ? $params['page_id'] : 1;
        $max_row = 20;
        
        if($params['search']['template'] == 0)
        {
            unset($params['search']['template']);
        }
        
        $pager   = new Model_Pager();
        $pager->getPagerInfo($page->getListCount($params['search']), $page_id, $max_row);
        $list = $page->getList($params['search'], 'id DESC', $max_row, ($max_row * ($page_id - 1)));
        $smarty->assign('list', $list);
        $smarty->assign('pager', $pager);
    }
    $template = $page->getTemplateMap();
    array_unshift($template, '指定なし');
    $smarty->assign('status', $page->getStatusMap());
    $smarty->assign('template', $template);
    $smarty->assign('search', $params['search']);
    $smarty->display('page/page_index.html');
}

// 新規登録
function op_regist($params, &$smarty, &$page)
{
    if (!isset($params['forms'])){
        $forms = array();
        foreach($page->getFormKeys() as $key){
            $forms[$key] = '';
        }

        //デフォルト値
        $forms["status"] = 0;
        
        $params['forms'] = $forms;
    }

    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('page/page_form.html');
}

function exec_validate_regist_form(&$params, &$smarty, &$page)
{
    $err = array();

    if ($page->validate($params['forms'], $err)){
        return exec_regist_form($params, $smarty, $page);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);

    return 'regist';
}
function exec_regist_form(&$params, &$smarty, &$page)
{   
    $params['forms']['st_time'] = $params['forms']['st_date'].' '.$params['forms']['st_time'];
    $params['forms']['ed_time'] = $params['forms']['ed_date'].' '.$params['forms']['ed_time'];
    
    $id = $page->insert($params['forms']);
    
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=regist_complete&id=' . $id);
    exit();
}

function op_regist_complete($params, &$smarty, &$page)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
    }
    $smarty->assign('message', '登録が完了しました');
    $smarty->display('page/page_form.html');
}

// 編集
function op_edit($params, &$smarty, &$page)
{
    if (!isset($params['id'])){
        __raise_error();
    }

    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $page->get($id);

        if($forms['st_time'] != '0000-00-00 00:00:00')
        {
            $forms['st_date'] = date('Y-m-d', strtotime($forms['st_time']));
            $forms['st_time'] = date('G', strtotime($forms['st_time']));
        }
        if($forms['ed_time'] != '0000-00-00 00:00:00')
        {
            $forms['ed_date'] = date('Y-m-d', strtotime($forms['ed_time']));
            $forms['ed_time'] = date('G', strtotime($forms['ed_time']));
        }
        
        $params['forms'] = $forms;
    }

    $smarty->assign('id', $id);
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('page/page_form.html');
}

function exec_validate_edit_form(&$params, &$smarty, &$page)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $err = array();
    if ($page->validate($params['forms'], $err)){
        return exec_edit_form($params, $smarty, $page);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'edit';
}
function exec_edit_form(&$params, &$smarty, &$page)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
        
    $params['forms']['st_time'] = $params['forms']['st_date'].' '.$params['forms']['st_time'];
    $params['forms']['ed_time'] = $params['forms']['ed_date'].' '.$params['forms']['ed_time'];
    
    $page->update($id, $params['forms']);

    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete&id=' . $id);
    exit();
}
function op_edit_complete($params, &$smarty, &$page)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
    }
    $smarty->assign('message', '更新が完了しました');
    $smarty->display('page/page_form.html');
}
?>